package com.cj.bigfileupload.common.i18n;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Locale;

/**
 * ClassName: MessageManager <br/>
 * Function: 多语言 <br/>
 * date: 2018/05/08 15:33 <br/>
 *
 * @author chenj
 * @version 1.0.0
 * @since JDK 1.8
 */
@Component
public class MessageManager implements Serializable {

    private static final long serialVersionUID = 2718646101027638752L;

    @Autowired
    private MessageSource messageSource;

    /**
     * 语言环境
     **/
    private Locale locale = Locale.CHINA;

    public void setLocale(Locale locale) {
        this.locale = locale;
    }

    public Locale getLocale() {
        return locale;
    }

    /**
     * 获取字符串资源值
     *
     * @param code 对应code值
     * @param args 格式化参数值
     * @return 字符串资源值
     */
    public String getMessage(String code, Object... args) {
        return getMessage(code, this.locale, args);
    }

    /**
     * 获取字符串资源值
     *
     * @param code   对应code值
     * @param locale 语言环境
     * @param args   格式化参数值
     * @return 字符串资源值
     */
    public String getMessage(String code, Locale locale, Object... args) {
        return messageSource.getMessage(code, args, locale);
    }
}
